package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@RequestMapping("/permission")
@Controller
public class PermissionController {


    @Reference
    private PermissionService permissionService;

    /**
     * 查询所有的权限
     * @param queryPageBean 分页查询的条件
     * @return
     */
    @RequestMapping("/findAll")
    @ResponseBody
    public PageResult findAll(@RequestBody QueryPageBean queryPageBean){

        PageResult pageResult= null;
        try {
            pageResult = permissionService.findAll(queryPageBean);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pageResult;
    }


    /**
     * 新建保存一个权限
     * @param permission 权限信息
     * @return
     */
    @RequestMapping("/saveOne")
    @ResponseBody
    public Result saveOne(@RequestBody Permission permission){

        try {
            permissionService.saveOne(permission);
            return new Result(true, "权限保存成功!!");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, "新建权限失败!");
    }

    /**
     * 根据id查询对应的权限信息
     * @param id 权限的id
     * @return
     */
    @RequestMapping("/selectById/{id}")
    @ResponseBody
    public Result selectById(@PathVariable int id){

        try {
            Permission permission= permissionService.selectById(id);
            if (permission!=null){
                return new Result(true, "权限查询成功!",permission);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, "权限查询失败,请刷新后在编辑!");
    }


    @RequestMapping("/updateOne")
    @ResponseBody
    public Result updateOne(@RequestBody Permission permission){
        try {
           permissionService.updateOne(permission);
            return new Result(true, "修改权限成功!");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, "修改权限列表失败!");
    }

    @RequestMapping("/deleteById/{id}")
    @ResponseBody
    public Result deleteById(@PathVariable int id){
        try {
            permissionService.deleteById(id);
            return new Result(true, "已成功删除此权限!");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, "删除此权限失败!");
    }

    @RequestMapping("/findAllNoPagination")
    @ResponseBody
    public Result findAllNoPagination(){
        try {
            List<Permission> list= permissionService.findAllNoPagination();
            return new Result(true, "查询所有权限成功!",list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, "查询所有权限失败");
    }

}
